//游戏数据
export default class GameData {
    private static instance: GameData = null;
    static DATA_KEY = {
        HISTORY_SCORE: 'historyScore',
        GAME_BOARD: 'gameBoard',
        USER_CONFIG: 'userConfig',
    }
    static getInstance() {
        if (!this.instance) {
            this.instance = new GameData;
            this.instance.init();
        }
        return this.instance;
    }
    historyScore: number = 0;//历史最高分
    curScore: number = 2;//当前分数
    boardArr: Array<Array<StepData>> = [];//当前盘面-二维
    boardList: StepData[] = [];//当前盘面-一维
    config: UserConfig = null;//用户配置信息
    itemColor = {
        2: '#FFFFFF',
        4: '#CCFFCC',
        8: '#B6D993',
        16: '#CCCCFF',
        32: '#CCCC99',
        64: '#E5AAE5',
        128: '#E58C8C',
        256: '#54D0A7',
        512: '#4EC3FE',
        1024: '#57D5D5',
        2048: '#6EADEB',
        'X': '#C4D1DE',
    };
    //初始化用户数据
    private init() {
        //最高分
        this.historyScore = this.getData(GameData.DATA_KEY.HISTORY_SCORE);
        this.historyScore = this.historyScore ? this.historyScore : 0;
        //配置信息
        this.config = new UserConfig;
        this.readUserConfig();
        //初始化盘面数组
        for (let m = 0; m < 4; m++) {
            this.boardArr[m] = [];
            for (let n = 0; n < 4; n++) {
                let step = new StepData;
                this.boardArr[m][n] = step;
                this.boardList.push(step);
            }
        }
    }
    //读取游戏配置
    readUserConfig() {
        let config: UserConfig = this.getData(GameData.DATA_KEY.USER_CONFIG);
        if (config) {
            for (let key in config) {
                if (config[key]) {
                    this.config[key] = config[key];
                }
            }
        }
    }
    saveScore() {
        if (this.curScore > this.historyScore) {
            this.historyScore = this.curScore;
            this.saveData(GameData.DATA_KEY.HISTORY_SCORE, this.curScore);
        }
    }
    saveData(key: string, data: string | Object | number) {
        if (data) {
            if (data instanceof Object) {
                data = JSON.stringify(data);
            }
            cc.sys.localStorage.setItem(key, data);
        }
    }
    getData(key: string) {
        let data = cc.sys.localStorage.getItem(key);
        if (data) {
            data = JSON.parse(data);
        }
        return data;
    }
    clearData(key: string) {
        cc.sys.localStorage.removeItem(key);
    }
}
export class moveCtrlStep {
    node: cc.Node;
    num: number | string;
    Spos: cc.Vec3;
    Epos: cc.Vec3;
    Sopa: number;
    Eopa: number;
}
export class StepData {
    x: number;
    y: number;
    num: number | string = 0;
    node: cc.Node;
}
export class UserConfig {
    isFast: boolean = false;
    isAnyStep: boolean = false;
}